package com.hzgj.bcl.util.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by wenfeng.dong on 2015/6/23.
 * Http请求操作工具类
 */
public class HttpUtils {

    private static final String ENCODETYPE="UTF-8";
    private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);
    /**
     * 采用Get请求方式对指定的服务地址进行请求，获取结果
     * @param serviceUrl
     * @param requestParam
     * @return
     */
    public static String sendGet(String serviceUrl,String requestParam) throws IOException {
        String result="";
        String requestUrl=serviceUrl;
        if (!requestParam.isEmpty()){
            if (!requestUrl.endsWith("?")){
                requestUrl=requestUrl+"?";
            }
            requestUrl=requestUrl+requestParam;
        }
        URL url = new URL(requestUrl);
        URLConnection connect = url.openConnection();
        connect.setUseCaches(false);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connect.getInputStream(),ENCODETYPE));

        StringBuilder sb = new StringBuilder();
        String line = null;
        try{
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        }catch (Exception ex){
            logger.error("Http Request Failed:"+ex.getMessage());
        } finally {
            try{
                reader.close();
                connect.getInputStream().close();
            }catch (Exception ex){
            }
        }
        result= sb.toString();
        return  result;
    }

    /**
     * 采用post请求方式对指定的服务地址进行请求，获取结果
     * @param serviceUrl
     * @param requestParam
     * @return
     */
    public static String sendPost(String serviceUrl,String requestParam) throws IOException {
        String result="";
        URL url = new URL(serviceUrl);
        HttpURLConnection connect = (HttpURLConnection) url.openConnection();
        connect.setDoOutput(true);
        connect.setRequestMethod("POST");
        connect.setUseCaches(false);
        connect.connect();
        //将对应的请求参数，写入到输入流中
        PrintWriter out = new PrintWriter(new OutputStreamWriter(connect.getOutputStream(),ENCODETYPE));
        out.write(requestParam);
        out.flush();
        out.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream(), ENCODETYPE));

        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            logger.error("Http Request Failed:"+e.getMessage());
        } finally {
            try {
                reader.close();
                connect.getInputStream().close();
            } catch (Exception e) {
            }
        }
        result = sb.toString();
        return  result;
    }
}
